﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WarehouseManagement.Views.Shared
{
    public partial class DateRange : UserControl
    {
        public enum DateViewStyle { DayView, MonthView, YearView }

        public DateRange()
        {
            InitializeComponent();
            _viewStyle = DateViewStyle.DayView;
        }

        public DateTime FromDate
        {
            get
            {
                return txtFromDate.DateTime;
            }
            set
            {
                txtFromDate.DateTime = value;
            }
        }

        private void txtToDate_EditValueChanged(object sender, EventArgs e)
        {
            if (FromDate > ToDate)
                FromDate = ToDate;

        }


        public DateTime ToDate
        {
            get
            {
                return txtToDate.DateTime;
            }
            set
            {
                txtToDate.DateTime = value;
            }
        }

        private void txtFromDate_EditValueChanged(object sender, EventArgs e)
        {
            if (FromDate > ToDate)
                ToDate = FromDate;
            //_presenter.LoadImportList();
        }

        private void cboOption_SelectedIndexChanged(object sender, EventArgs e)
        {
            AssignDate();
        }

        private void AssignDate()
        {
            var today = DateTime.Now;
            switch (cboOption.SelectedIndex)
            {
                case 1:
                    {
                        DateTime startOfWeek = DateTime.Today.AddDays(-1 * (int)(DateTime.Today.DayOfWeek));
                        this.FromDate = startOfWeek;
                        this.ToDate = startOfWeek.AddDays(6);
                        break;
                    }
                case 0:
                    {
                        this.FromDate = today;
                        this.ToDate = FromDate;
                        break;
                    }
                case 2:
                    {
                        this.FromDate = new DateTime(today.Year, today.Month, 1);
                        this.ToDate = new DateTime(today.Year, today.Month, DateTime.DaysInMonth(today.Year, today.Month));
                        break;
                    }
                case 3:
                    {
                        var period = GetPeriod(today.Month);
                        this.FromDate = GetStartDayOfPeriod(period, today.Year);
                        var todate = this.FromDate.AddMonths(2);
                        this.ToDate = new DateTime(todate.Year, todate.Month, DateTime.DaysInMonth(todate.Year, todate.Month));
                        break;
                    }
                case 4:
                    {
                        this.FromDate = new DateTime(today.Year, 1, 1);
                        this.ToDate = new DateTime(today.Year, 12, DateTime.DaysInMonth(today.Year, 12));
                        break;
                    }

                case 5:
                    {
                        // Thang 1
                        this.FromDate = new DateTime(today.Year, 1, 1);
                        this.ToDate = new DateTime(today.Year, 1, DateTime.DaysInMonth(today.Year, 1));
                        break;
                    }
                case 6:
                    {
                        // Thang 1
                        this.FromDate = new DateTime(today.Year, 2, 1);
                        this.ToDate = new DateTime(today.Year, 2, DateTime.DaysInMonth(today.Year, 2));
                        break;
                    }
                case 7:
                    {
                        // Thang 1
                        this.FromDate = new DateTime(today.Year, 3, 1);
                        this.ToDate = new DateTime(today.Year, 3, DateTime.DaysInMonth(today.Year, 3));
                        break;
                    }
                case 8:
                    {
                        // Thang 1
                        this.FromDate = new DateTime(today.Year, 4, 1);
                        this.ToDate = new DateTime(today.Year, 4, DateTime.DaysInMonth(today.Year, 4));
                        break;
                    }
                case 9:
                    {
                        // Thang 1
                        this.FromDate = new DateTime(today.Year, 5, 1);
                        this.ToDate = new DateTime(today.Year, 5, DateTime.DaysInMonth(today.Year, 5));
                        break;
                    }
                case 10:
                    {
                        // Thang 1
                        this.FromDate = new DateTime(today.Year, 6, 1);
                        this.ToDate = new DateTime(today.Year, 6, DateTime.DaysInMonth(today.Year, 6));
                        break;
                    }
                case 11:
                    {
                        // Thang 1
                        this.FromDate = new DateTime(today.Year, 7, 1);
                        this.ToDate = new DateTime(today.Year, 7, DateTime.DaysInMonth(today.Year, 7));
                        break;
                    }
                case 12:
                    {
                        // Thang 1
                        this.FromDate = new DateTime(today.Year, 8, 1);
                        this.ToDate = new DateTime(today.Year, 8, DateTime.DaysInMonth(today.Year, 8));
                        break;
                    }
                case 13:
                    {
                        // Thang 1
                        this.FromDate = new DateTime(today.Year, 9, 1);
                        this.ToDate = new DateTime(today.Year, 9, DateTime.DaysInMonth(today.Year, 9));
                        break;
                    }
                case 14:
                    {
                        // Thang 1
                        this.FromDate = new DateTime(today.Year, 10, 1);
                        this.ToDate = new DateTime(today.Year, 10, DateTime.DaysInMonth(today.Year, 10));
                        break;
                    }
                case 15:
                    {
                        // Thang 11
                        this.FromDate = new DateTime(today.Year, 11, 1);
                        this.ToDate = new DateTime(today.Year, 11, DateTime.DaysInMonth(today.Year, 11));
                        break;
                    }
                case 16:
                    {
                        // Thang 12
                        this.FromDate = new DateTime(today.Year, 12, 1);
                        this.ToDate = new DateTime(today.Year, 12, DateTime.DaysInMonth(today.Year, 12));
                        break;
                    }

                case 17:
                    {
                        // Quy 1
                        this.FromDate = GetStartDayOfPeriod(1, today.Year);
                        var todate = FromDate.AddMonths(2);
                        this.ToDate = new DateTime(todate.Year, todate.Month, DateTime.DaysInMonth(todate.Year, todate.Month));
                        break;
                    }
                case 18:
                    {
                        // Quy 2
                        this.FromDate = GetStartDayOfPeriod(2, today.Year);
                        var todate = FromDate.AddMonths(2);
                        this.ToDate = new DateTime(todate.Year, todate.Month, DateTime.DaysInMonth(todate.Year, todate.Month));
                        break;
                    }
                case 19:
                    {
                        // Quy 3
                        this.FromDate = GetStartDayOfPeriod(3, today.Year);
                        var todate = FromDate.AddMonths(2);
                        this.ToDate = new DateTime(todate.Year, todate.Month, DateTime.DaysInMonth(todate.Year, todate.Month));
                        break;
                    }
                case 20:
                    {
                        // Quy 4
                        this.FromDate = GetStartDayOfPeriod(4, today.Year);
                        var todate = FromDate.AddMonths(2);
                        this.ToDate = new DateTime(todate.Year, todate.Month, DateTime.DaysInMonth(todate.Year, todate.Month));
                        break;
                    }
            }
        }

        private int GetPeriod(int month)
        {
            if (month >= 1 && month <= 3)
                return 1;
            else if (month >= 4 && month <= 6)
                return 2;
            else if (month >= 7 && month <= 9)
                return 3;
            else
                return 4;
        }

        private DateTime GetStartDayOfPeriod(int period, int year)
        {
            if (period < 1 || period > 4)
                throw new ArgumentException("Period invalid");
            return new DateTime(year, (period * 3) - 2, 1);
        }

        private void DateRange_Load(object sender, EventArgs e)
        {
            cboOption_SelectedIndexChanged(this, e);
        }

        private DateViewStyle _viewStyle;
        public DateViewStyle ViewStyle
        {
            get
            {
                return _viewStyle;
            }
            set
            {
                _viewStyle = value;
                switch (_viewStyle)
                {
                    case DateViewStyle.MonthView:
                        {
                            cboOption.SelectedIndex = 4;
                            txtFromDate.Properties.VistaCalendarViewStyle = DevExpress.XtraEditors.VistaCalendarViewStyle.YearView;
                            txtFromDate.Properties.DisplayFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
                            txtFromDate.Properties.DisplayFormat.FormatString = "MM/yyyy";
                            txtFromDate.Properties.EditMask = "MM/yyyy";

                            txtToDate.Properties.VistaCalendarViewStyle = DevExpress.XtraEditors.VistaCalendarViewStyle.YearView;
                            txtToDate.Properties.DisplayFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
                            txtToDate.Properties.DisplayFormat.FormatString = "MM/yyyy";
                            txtToDate.Properties.EditMask = "MM/yyyy";

                            break;
                        }
                    case DateViewStyle.YearView:
                        {
                            txtFromDate.Properties.VistaCalendarViewStyle = DevExpress.XtraEditors.VistaCalendarViewStyle.YearsGroupView;
                            txtFromDate.Properties.DisplayFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
                            txtFromDate.Properties.DisplayFormat.FormatString = "yyyy";
                            txtFromDate.Properties.EditMask = "yyyy";

                            txtToDate.Properties.VistaCalendarViewStyle = DevExpress.XtraEditors.VistaCalendarViewStyle.YearsGroupView;
                            txtToDate.Properties.DisplayFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
                            txtToDate.Properties.DisplayFormat.FormatString = "yyyy";
                            txtToDate.Properties.EditMask = "yyyy";

                            break;
                        }
                    default:
                        {
                            cboOption.SelectedIndex = 2;
                            txtFromDate.Properties.VistaCalendarViewStyle = DevExpress.XtraEditors.VistaCalendarViewStyle.Default;
                            txtFromDate.Properties.DisplayFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
                            txtFromDate.Properties.DisplayFormat.FormatString = "dd/MM/yyyy";
                            txtFromDate.Properties.EditMask = "dd/MM/yyyy";

                            txtToDate.Properties.VistaCalendarViewStyle = DevExpress.XtraEditors.VistaCalendarViewStyle.Default;
                            txtToDate.Properties.DisplayFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
                            txtToDate.Properties.DisplayFormat.FormatString = "dd/MM/yyyy";
                            txtToDate.Properties.EditMask = "dd/MM/yyyy";
                            break;
                        }
                }
            }
        }
    }
}
